<template>
	<div class="app-container">
		<jk-operation-card ref="box1" class="margin-bottom-10">
			<div class="flex-between-center-wrap">
				<div class="flex-start-center-wrap">
					<el-radio-group v-model="viewType" size="small">
						<el-radio-button label="list-view">
							<span class="el-icon-c-scale-to-original"></span>
							<span>效率前五列表</span>
						</el-radio-button>
						<el-radio-button label="chart-bottom-view">
							<span class="el-icon-s-data"></span>
							<span>效率低前五列表</span>
						</el-radio-button>
					</el-radio-group>
				</div>
				<div class="flex-end-center-wrap">
					<div class="flex-end-center-wrap">
						<jk-select v-model="searchParams.workshopId" :option-list="workshopList" clearable placeholder="区域" size="small" class="search-bar-item-size1 margin-5-0 item-gap" @change="areaOnChange"></jk-select>
						<jk-select v-model="searchParams.processCode" clearable placeholder="工序" value-key-name="code" :option-list="garbledProcess" size="small" class="search-bar-item-size1 margin-5-0 item-gap"></jk-select>
						<jk-date-picker v-model="searchParams.belongDate" :clearable="false" placeholder="日期" :picker-options="pickerOptions" size="small" class="search-bar-item-size2 margin-5-0 item-gap"></jk-date-picker>
						<jk-select v-model="searchParams.shiftId" clearable placeholder="班次" :option-list="shiftList" size="small" class="search-bar-item-size1 margin-5-0 item-gap" @change="shiftOnChange"></jk-select>
					</div>
					<jk-search-button @click="onSearchBtn"></jk-search-button>
				</div>
			</div>
		</jk-operation-card>

		<jk-card>
			<component
				:is="viewType"
				:table-data="tableData"
				:height="tableHeight + 90"
				@getListRequest="getListRequest"
			></component>
		</jk-card>
	</div>
</template>

<script>
    import calHeight from '/src/mixins/cal-table-height';
    import { pageConfig, formatMonth, formatDate, tipsBar } from '/src/utils/jk/jk-tool';
    import getMenuColumn from '../../../mixins/getMenuColumn';
    import handlerCache from '../../../mixins/handler-cache';
    import { workforcemanagementShiftPage } from '/src/api/schedule/shift';
    import { productionPrdReportMachineRank } from '../../../api/production/PrdReport';
    import listView from './components/list-view.vue';
    import chartBottomView from './components/chart-bottom-view.vue';
    export default {
        name: 'PrdReport',
        components: { listView, chartBottomView },
        mixins: [calHeight, getMenuColumn, handlerCache],
        data() {
            return {
                apiItem: {
                    'page': productionPrdReportMachineRank
                },
                pickerOptions: {
                    disabledDate(time) {
                        return time.getTime() > Date.now();
                    }
                },
                viewType: 'list-view',
                processList: [],
                garbledProcess: [],
                tableData: {},
                shiftList: [],
                workshopList: [],
                tableLoading: true,
                searchParams: {
                    belongDate: '',
                    count: 0,
                    processCode: '',
                    shiftId: '',
                    shiftName: '',
                    workshopId: null,
                    pageNo: pageConfig.pageNo,
                    pageSize: pageConfig.pageSize
                }
            };
        },
        mounted() {
            this.getDependentData();
        },
        methods: {
            getProcessData(workshopId) {
                this.garbledProcess = [];
                this.processList.forEach(y => {
                    if (workshopId === y.workshopId) {
                        this.garbledProcess.push(y);
                    }
                });
            },
            areaOnChange(workshopId) {
                this.$set(this.searchParams, 'shiftId', '');
                this.$set(this.searchParams, 'shiftCode', '');
                this.$set(this.searchParams, 'shiftName', '');
                this.$set(this.searchParams, 'processId', '');
                this.$set(this.searchParams, 'processCode', '');
                this.$set(this.searchParams, 'processName', '');
                if (!this.searchParams.workshopId) {
                    this.shiftList = [];
                } else {
                    this.getShiftListRequest();
                }
                this.getProcessData(workshopId);
            },
            shiftOnChange(val) {
                this.shiftList.find(x => {
                    if (x.id === val) {
                        this.searchParams.shiftName = x.name;
                    }
                }
                );
            },
            // 获取所有班次
            getShiftListRequest() {
                const params = JSON.parse(JSON.stringify(this.searchParams));
                delete params.pageNo;
                delete params.pageSize;
                delete params.shiftId;
                delete params.count;
                delete params.yarnCount;
                delete params.belongDate;
                delete params.processCode;
                params.auditState = 1;
                return workforcemanagementShiftPage(params).then(res => {
                    if (res.data.status === 200) {
                        this.shiftList = res.data.res || [];
                    }
                });
            },
            async getDependentData() {
                this.tableLoading = true;
                if (!this.searchParams.belongDate) {
                    this.searchParams.belongDate = formatDate(Date.now());
                }
                this.workshopList = this.$store.state.user.userWorkshop.workshopList;
                this.processList = this.$store.state.user.userProcess.processList;
                // 默认值(页面缓存时，没有值的情况下再赋值)
                if (!this.searchParams.processCode) {
                    this.$set(this.searchParams, 'processCode', this.$store.state.user.userProcess.defaultProcessCode || '');
                }
                if (!this.searchParams.workshopId) {
                    this.$set(this.searchParams, 'workshopId', this.$store.state.user.userWorkshop.defaultWorkshopId);
                }
                this.getProcessData(this.$store.state.user.userWorkshop.defaultWorkshopId);
                await this.getShiftListRequest();
                if (!this.searchParams.shiftId) {
                    this.$set(this.searchParams, 'shiftId', this.shiftList && this.shiftList[0].id ? this.shiftList[0].id : null);
                }
                if (!this.searchParams.shiftName) {
                    this.$set(this.searchParams, 'shiftName', this.shiftList && this.shiftList[0].name ? this.shiftList[0].name : null);
                }
                await this.getMenuColumnListRequest();
                await this.getListRequest();
                this.tableLoading = false;
            },
            /** 查询列表 */
            getListRequest() {
                this.tableLoading = true;
                const params = JSON.parse(JSON.stringify(this.searchParams));
                return this.apiItem['page'](params).then(res => {
                    this.tableLoading = false;
                    if (res.data.status === 200) {
                        this.tableData = res.data.res ? res.data.res : {};
                    }
                });
            },
            /** 搜索按钮操作 */
            onSearchBtn() {
                this.searchParams.pageNo = 1;
                this.searchParams.count = 1;
                this.getListRequest();
            }
        }
    };
</script>
<style>
.machine-group-item {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  border-left: solid 1px #eee;
  border-top: solid 1px #eee;
  width: 80px;
  overflow: hidden;
  text-overflow: ellipsis;
  padding: 0 4px;
  flex-shrink: 0;
}
</style>
